package com.lyw.forum.common.utils;


import com.lyw.forum.common.enums.ResultCode;
import com.lyw.forum.common.exception.ApplicationException;
import com.lyw.forum.pojo.dataobject.*;
import com.lyw.forum.pojo.request.*;
import com.lyw.forum.pojo.response.*;
import com.lyw.forum.pojo.response.BoardSelectResponse;
import org.springframework.beans.BeanUtils;

import java.util.List;

public class BeanTransUtils {

    public static User transUser(UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null){
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        User user = new User();
        BeanUtils.copyProperties(userRegisterRequest, user);
        return user;
    }

    public static UserSelectResponse transUser(User user) {
        if (user == null) {
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        UserSelectResponse userSelectResponse = new UserSelectResponse();
        BeanUtils.copyProperties(user, userSelectResponse);

        return userSelectResponse;
    }

    public static List<BoardSelectResponse> transBoard(List<Board> boards) {
        if (boards == null) {
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        return boards.stream().map(board -> {
            BoardSelectResponse boardSelectResponse = new BoardSelectResponse();
            BeanUtils.copyProperties(board, boardSelectResponse);
            return boardSelectResponse;
        }).toList();

    }

    public static Article transArticleCreateRequest(ArticleCreateRequest articleCreateRequest) {
        if (articleCreateRequest == null) {
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Article article = new Article();
        BeanUtils.copyProperties(articleCreateRequest, article);

        return article;
    }

    public static BoardSelectResponse transToBoardSelectResponse(Board board) {
        if (board == null) {
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        BoardSelectResponse boardSelectResponse = new BoardSelectResponse();
        BeanUtils.copyProperties(board, boardSelectResponse);

        return boardSelectResponse;
    }

    public static List<ArticleSelectResponse> transArticle(List<Article> articles) {
        if (articles == null) {
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        return articles.stream().map(article -> {
            ArticleSelectResponse articleSelectResponse = new ArticleSelectResponse();
            BeanUtils.copyProperties(article, articleSelectResponse);
            return articleSelectResponse;
        }).toList();
    }

    public static ArticleSelectResponse transArticle(Article articles) {
        if (articles == null) {
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        ArticleSelectResponse articleSelectResponse = new ArticleSelectResponse();
        BeanUtils.copyProperties(articles, articleSelectResponse);

        return articleSelectResponse;
    }

    public static Article transArticleUpdateRequest(ArticleUpdateRequest articleUpdateRequest) {
        if (articleUpdateRequest == null) {
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Article article = new Article();
        BeanUtils.copyProperties(articleUpdateRequest, article);

        return article;
    }

    public static ArticleReply transArticleReplyCreateRequest(ArticleReplyCreateRequest articleReplyCreateRequest) {
        if (articleReplyCreateRequest == null) {
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        ArticleReply articleReply = new ArticleReply();
        BeanUtils.copyProperties(articleReplyCreateRequest, articleReply);

        return articleReply;
    }

    public static List<ArticleReplySelectResponse> transArticleReply(List<ArticleReply> articleReplies) {
        if (articleReplies == null) {
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        List<ArticleReplySelectResponse> articleReplySelectResponses = articleReplies.stream().map(articleReply -> {
            ArticleReplySelectResponse articleReplySelectResponse = new ArticleReplySelectResponse();
            BeanUtils.copyProperties(articleReply, articleReplySelectResponse);
            return articleReplySelectResponse;
        }).toList();

        return articleReplySelectResponses;
    }

    public static Message transMessageCreateRequest(MessageCreateRequest messageCreateRequest) {
        if (messageCreateRequest == null) {
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Message message = new Message();
        BeanUtils.copyProperties(messageCreateRequest, message);

        return message;
    }

    public static List<MessageSelectResponse> transMessage(List<Message> messages) {
        if (messages == null) {
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        List<MessageSelectResponse> messageSelectResponses = messages.stream().map(message -> {
            MessageSelectResponse messageSelectResponse = new MessageSelectResponse();
            BeanUtils.copyProperties(message, messageSelectResponse);
            return messageSelectResponse;
        }).toList();

        return messageSelectResponses;
    }

    public static MessageSelectResponse transMessage(Message message) {
        if (message == null) {
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        MessageSelectResponse messageSelectResponse = new MessageSelectResponse();
        BeanUtils.copyProperties(message, messageSelectResponse);

        return messageSelectResponse;
    }
}
